{extend name="public:base"}
{block name='css'}
<link href="__PUBLIC__/css/plugins/iCheck/custom.css" rel="stylesheet">
{/block}
{block name="js"}
<script type="text/javascript" charset="utf-8" src="__PUBLIC__/ueditor/ueditor.config.js"></script>
<script type="text/javascript" charset="utf-8" src="__PUBLIC__/ueditor/ueditor.all.min.js"> </script>
<script type="text/javascript" charset="utf-8" src="__PUBLIC__/ueditor/lang/zh-cn/zh-cn.js"></script>
<script type="text/javascript">
    var ue = UE.getEditor('editor');
</script>

<script src="__PUBLIC__/js/content.min.js?v=1.0.0"></script>
<script src="__PUBLIC__/js/plugins/validate/jquery.validate.min.js"></script>
<script src="__PUBLIC__/js/plugins/validate/messages_zh.min.js"></script>

<script type="text/javascript">
    $.validator.setDefaults({
        highlight: function(e) {
            $(e).closest(".form-group").removeClass("has-success").addClass("has-error")
        },
        success: function(e) {
            e.closest(".form-group").removeClass("has-error").addClass("has-success")
        },
        errorElement: "span",
        errorPlacement: function(e, r) {
            e.appendTo(r.is(":radio") || r.is(":checkbox") ? r.parent().parent().parent() : r.parent())
        },
        errorClass: "help-block m-b-none",
        validClass: "help-block m-b-none"
    });
    $().ready(function() {
        var e = "<i class='fa fa-times-circle'></i> ";
        $("#commentForm").validate({
            rules: {
                title: "required"
            },
            messages: {
                title: e + "请输入标题"
            },
            submitHandler: function(form){
                if ($(form).hasClass('ed')) {
                    return false;
                }

                $(form).addClass('ed');
                var obj = showMyLoading();
                $.post('', $(form).serialize(), function(data){
                    hideMyLoading(obj);
                    layer.msg(data.info);
                    if (data.status == 'y') {
                        setTimeout(function(){window.location.href = '{:url("index", ["newclass_id"=>$newclass_id])}';},1500);
                    } else {
                        $(form).removeClass('ed');
                    }
                }, 'json');
            },
            invalidHandler: function(form, validator) {  //不通过回调
                return false;
            }
        })
    });

    function preview(self) {
        $(self).unbind('change').change(function (event) {
            var self = this;
            var files = event.target.files || event.dataTransfer.files;
            var formData = new FormData();
            formData.append('imgFile', files[0]);
            var ajax = new XMLHttpRequest();
            ajax.open("POST", "{:url('index/Index/saveImg')}", true);
            ajax.send(formData);
            ajax.onreadystatechange = function () {
                if (ajax.readyState == 4) {
                    if (ajax.status >= 200 && ajax.status < 300 || ajax.status == 304) {
                        var data = JSON.parse(ajax.responseText);
                        if (data.status == 'y') {
                            $(self).next().val(data.data.src);
                            $(self).next().next().attr('href', data.data.src).show().children().attr('src', data.data.src);
                        } else {
                            layer.msg(data.info);
                        }
                    }
                }
            }
        });
    }
</script>
{/block}
{block name='body'}
<body class="gray-bg">
    <div class="wrapper wrapper-content animated fadeInRight">
        
        <div class="row">
            <div class="col-sm-12">
                <div class="ibox float-e-margins">
                    <div class="ibox-title">
                        <h5>{empty name='data'}添加{else/}修改{/empty}文章</h5>
                        <a href="{:url('index',['newclass_id'=>$newclass_id])}" class="btn btn-sm btn-primary pull-right m-t-n-xs">返回</a>
                    </div>
                    <div class="ibox-content">
						
                        <form method="post" class="form-horizontal" id="commentForm" action="" enctype="multipart/form-data" >
							<input type="hidden" value="{$newclass_id}" name="newclass_id" />
                            <input type="hidden" value="{$data.id|default=''}" name="id" />
                            <div class="form-group">
                                <label class="col-sm-2 control-label">标题</label>
                                <div class="col-sm-8">
                                    <input type="text" name="title" value="{$data.title|default=''}" placeholder="请输入标题" class="input-sm form-control">
                                </div>
                            </div>
                            <div class="form-group">
                                <label class="col-sm-2 control-label">图片</label>
                                <div class="col-sm-8">
                                    <input type="file" onclick="preview(this);" class="input-sm form-control">
                                    <input type="hidden" name="img" value="{$data.img|default=''}">
                                    <a href="{$data.img|default=''}" target="_blank" {if condition='empty($data.img)'}style="display: none;"{/if}><img src="{$data.img|default=''}" style="max-width:100px;max-height:100px;"></a>
                                </div>
                            </div>
                            <div class="form-group">
                                <label class="col-sm-2 control-label">内容</label>
                                <div class="col-sm-8">
                                    <script id="editor" name="content" type="text/plain" style="width:900px;max-width:100%;height:300px;">{notempty name="data.content"}{$data.content|htmlspecialchars_decode}{/notempty}</script>
                                </div>
                            </div>
							<div class="form-group">
                                <label class="col-sm-2 control-label">关键字</label>
                                <div class="col-sm-8">
                                    <input type="text" name="keywords" value="{$data.keywords|default=''}" placeholder="请输入关键字" class="input-sm form-control">
                                </div>
                            </div>
							<div class="form-group">
                                <label class="col-sm-2 control-label">描述</label>
                                <div class="col-sm-8">
                                    <textarea name="description" class="input-sm form-control">{$data.description|default=''}</textarea>
                                </div>
                            </div>
                            <div class="hr-line-dashed"></div>
                            <div class="form-group">
                                <div class="col-sm-4 col-sm-offset-2">
                                    <button class="btn btn-primary" type="submit">保存内容</button>
                                </div>
                            </div>
                        </form>
                    </div>
                </div>
            </div>
        </div>
    </div>

</body>
{/block}
